﻿#region

using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls;
using Radovici.Ctrls.Hacks;
using YouTubePlaylist.Models;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class PlaylistsSearchCtrl : IPlaylistsCtrl
    {
        #region Private fields

        private readonly Timer _findPlaylistsTimer;
        private PlaylistsViewModel _vm;

        #endregion

        public static readonly DependencyProperty PlaylistsModelProperty = DependencyProperty.Register(
            "PlaylistsModel", typeof (PlaylistsModel), typeof (PlaylistsSearchCtrl),
            new PropertyMetadata(null, OnPlaylistsModelChanged));

        public PlaylistsSearchCtrl()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            _findPlaylistsTimer = new Timer(OnFindPlaylistsCallback, null, int.MaxValue, int.MaxValue);
            ViewModel = new PlaylistsViewModel();
        }

        #region IPlaylistsCtrl Members

        public PlaylistsViewModel ViewModel
        {
            get { return _vm; }
            private set
            {
                _vm = value;
                var element = Content as FrameworkElement;
                if (element != null)
                {
                    element.DataContext = _vm;
                }
            }
        }

        public PlaylistsModel PlaylistsModel
        {
            get { return (PlaylistsModel) GetValue(PlaylistsModelProperty); }
            set { SetValue(PlaylistsModelProperty, value); }
        }

        #endregion

        private static void OnPlaylistsModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as PlaylistsSearchCtrl;
            if (ctrl != null)
            {
                ctrl.ViewModel.PlaylistsModel = e.NewValue as PlaylistsModel;
            }
        }

        #region Private methods

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            //Options.ApplyTheme("RainierOrange.xaml");
            //this.ApplyTheme();
        }

        private void OnSearchPlaylistChanged(object sender, TextChangedEventArgs e)
        {
            _findPlaylistsTimer.Change(1000, int.MaxValue);
        }

        private void OnFindPlaylistsCallback(object state)
        {
            //Dispatcher.BeginInvoke(delegate { ViewModel.PlaylistsModel.FindPlaylists(SearchString.Text); }); //NOTE: Obsoleted for FinderCtrl's SearchModel
        }

        private void OnDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var data = sender as DataGrid;
            if (data != null)
            {
                var p = data.SelectedItem as VideoPlaylist;
                if (p != null)
                {
                    ViewModel.PlaylistsModel.Current =
                        ViewModel.PlaylistsModel.PlaylistModels.Single(vm => vm.Playlist.Id == p.Id);
                    ViewModel.PlaylistsModel.Model.AddPlaylist(ViewModel.PlaylistsModel.Current.Playlist,
                                                               ViewModel.PlaylistsModel.Playlists);
                }
            }
        }

        private void OnPlaylistSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.PlaylistsModel.SelectedPlaylist = grid.SelectedItem as VideoPlaylist;
            }
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.PlaylistsModel.SelectedPlaylist = grid.SelectedItem as VideoPlaylist;
            }
        }

        #endregion
    }
}